function showMember() {
	mask.open(true);
	ajaxGet(website + "boMember/show", null, function(result) {
		var tabOrder = $("#currentTabOrder").val();
		$("#tabs-"+tabOrder).html(result);
		initMember();
		mask.close();
	}, "html");
}

function initMember() {
	$("#query-member").button().click(function() {
		searchMember();
	});
	
	$("#create-member").button().click(function() {
		addOrEditMemberDialog(ACTION_ADD);
	});
	
	$("#edit-member").button().click(function() {
		addOrEditMemberDialog(ACTION_EDIT);
	});

	$("#delete-member").button().click(function() {
		openDeleteDialog(MEMBER,"checkKey");
	});
	
	$(".bindSupplierBtn").button().click(function() {
		bindSupplierDialog($(this).attr("memberid"));
	});
	
	$(".editMemberBtn").button().click(function() {
		addOrEditMemberDialog(ACTION_EDIT, $(this).attr("memberid"));
	});

	$(".delMemberBtn").button().click(function() {
		openDeleteDialog(MEMBER,"checkKey",$(this).attr("memberid"));
	});
}

function bindSupplierDialog(memberid){
	mask.open(true);
	var params = {
		memberid : memberid
	};
	$("#currentMember").val(memberid);
	ajaxGet(website + "boMember/bindPage", params, function(result) {
		$("#bindDialog").html(result);
		$("#bindDialog").dialog({
			width : 450,
			modal : true,
			resizable : false,
			buttons : {
				"取消" : function() {
					$(this).dialog("close");
				}
			},
			close : function() {
				$(this).html("");
			}
		});
		mask.close();
	}, "html");
}

function bindSupplier(){
	var params = {
		memberid: $("#currentMember").val(),
		supid: $( "#combobox" ).val()
	}
	mask.open(true);
	ajaxGet(website + "boMember/bindSupplier", params, function(result) {
		$("#bindDialog").html(result);
		mask.close();
	}, "html");
}

function unBindSupplier(supid){
	var params = {
		memberid: $("#currentMember").val(),
		supid: supid
	}
	mask.open(true);
	ajaxGet(website + "boMember/unBindSupplier", params, function(result) {
		$("#bindDialog").html(result);
		mask.close();
	}, "html");
}

function addOrEditMemberDialog(action, updateId) {
	var url = website + "boMember/insertOrUpdatePage";
	var params = null;
	if(action == ACTION_EDIT){
		var ids = null;
		if(updateId == null){
			ids = getSelectedItems("checkKey");
			if (ids == "" || ids.split(",").length != 1) {
				alert("请只选择一个会员");
				return;
			}
		}else{
			ids = updateId;
		}
		params = {
			memberid : ids
		}
	}
	
	$("#curAction").val(action);
	
	mask.open(true);
	ajaxGet(url, params, function(result) { 
		$("#dialog-form").html(result);
		$("#dialog-form") .dialog({
			width : 450,
			modal : true,
			resizable : false,
			buttons : {
				"确定" : function() {
					saveMember();
				},
				"取消" : function() {
					$(this).dialog("close");
				}
			},
			close : function() {
				allFields.val("").removeClass("ui-state-error");
				$(this).html("");
			}
		});
		mask.close();
	}, "html");
}


function saveMember(){
	var username = $("#username");
	var password = $("#password");
	var allFields = $([]).add(username).add(password), tips = $(".validateTips");
	
	var bValid = true;
	allFields.removeClass("ui-state-error");
	if($("#curAction").val() == ACTION_ADD){
		bValid = bValid&& checkLength(username, "用户名",1, 20, tips);
		bValid = bValid&& checkLength(password, "密码",1, 50, tips);
	}
	if (bValid) {
		$("#memberForm").submit();
	}
}

function searchMember(){
	searchMemberWithPage();
}

function getPage(pageIndex){
	searchMemberWithPage(pageIndex);
}

function searchMemberWithPage(pageIndex){
	var params = {
		queryType : $("#queryType").val(),
		queryText : $("#queryText").val()
	};
	if(pageIndex != null){
		params["page"] = pageIndex;
	}
	mask.open(true);
	ajaxGet(website + "boMember/show",params,function(result){
		var tabOrder = $("#currentTabOrder").val();
		$("#tabs-"+tabOrder).html(result);
		initMember();
		mask.close();
	},"html");
}